//
//  MySetingViewController.swift
//  RoundCatPlay2
//
//  Created by 姜澎 on 2019/10/24.
//  Copyright © 2019 jiangpeng. All rights reserved.
//

import UIKit

class MySetingViewController: UIViewController {
    let tableSourse:[String] = ["头像","昵称","性别","星座","生日","所在地","手机号"]
    //    左侧设置按钮
    private lazy var setFX:UIButton = {
        var btu = UIButton(type: .custom)
        
        var image = UIImage(named: "分享")
        btu.imageView?.contentMode = .scaleAspectFill
        btu.setImage(image, for: .normal)
        btu.setImage(image, for: .highlighted)
        btu.addTarget(self, action: #selector(fxFunc), for: .touchUpInside)
        btu.frame = CGRect(origin: .zero, size: CGSize(width: 26, height: 25))
        btu.tag = 1
        return btu
    }()
    
    private lazy var backgroundImage:UIImageView = {
        var imageV = UIImageView()
        imageV.contentMode = .scaleAspectFill
        imageV.image = UIImage(named: "MybackgrogroundImage2")
        return imageV
    }()
    
    private lazy var titleImage: UIImageView = {
        let im = UIImageView()
        im.image = UIImage(named: "myTitle")
        im.layer.cornerRadius = 10
        return im
    }()
    
    private lazy var bottomBut :UIButton = {
        var but = UIButton(type: .custom)
        but.frame = CGRect(x: 20, y: 10, width: UIScreen.main.bounds.width - 40 , height: 40)
        but.setTitle("保存", for: .normal)
        but.setTitle("保存", for: .highlighted)
        but.backgroundColor = UIColor(red: 64/255, green: 78/255, blue: 232/255, alpha: 1)
        but.layer.cornerRadius = 5
        return but
    }()
//    按钮
    private lazy var footView :UIView = {
        let v = UIView()
        v.addSubview(self.bottomBut)
        v.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50)
        return v
    }()
    
    private lazy var tvcView: UIView = {
        let v = UIView()
        v.addSubview(self.setMyTvc)
        v.backgroundColor = .white
        v.layer.cornerRadius = 20
        return v
    }()
    
    private lazy var setMyTvc: UITableView = {
        var t = UITableView()
        t.dataSource = self
        t.delegate = self
        t.bounces = false
        return t
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.white
        title = "个人设置"
        navigationController?.navigationBar.tintColor = .white
        self.navigationController?.navigationBar.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor : UIColor.white,
            NSAttributedString.Key.font : UIFont.italicSystemFont(ofSize: 20)
        ]
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: setFX)
        
        setUI()
        
    }
    
    
    @objc func fxFunc() {
        print("fsda")
//         self.tvcView.removeFromSuperview()
//        self.tvcView.addSubview(UIView())
        
        
        for i in self.tvcView.subviews {
            if i == self.setMyTvc {
               print(i)
                i.backgroundColor = .red
                self.tvcView.removeFromSuperview()
            }
        }
        
    }
    
    
}

extension MySetingViewController :UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tableSourse.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "mysetScell")
        if cell == nil {
            cell = UITableViewCell.init(style: .value1, reuseIdentifier: "mysetScell")
        }
    
            cell?.textLabel?.text = self.tableSourse[indexPath.row]
            cell?.detailTextLabel?.text = "选择"
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 18)
            cell?.detailTextLabel?.font = UIFont.systemFont(ofSize: 18)
            cell?.accessoryType = .disclosureIndicator
            cell?.selectionStyle = .none
        
        return cell!
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return self.footView
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 50
    }
    
}

extension MySetingViewController {
    func setUI(){
        view.addSubview(backgroundImage)
        view.addSubview(tvcView)
        view.addSubview(titleImage)
        setBackgroundImageFrame()
        setTableViewFrame()
        setTitleImageFrame()
        setTvcFrame()
    }
    
    func setBackgroundImageFrame(){
        self.backgroundImage.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(200)
        }
    }
    
    func setTableViewFrame(){
        self.tvcView.snp.makeConstraints { (make) in
            make.top.equalTo(self.backgroundImage.snp_bottom).offset(-50)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
        }
    }
    
    func setTitleImageFrame() {
        self.titleImage.snp.makeConstraints { (make) in
            make.width.equalTo(100)
            make.height.equalTo(100)
            make.centerY.equalTo(tvcView.snp_top)
            make.centerX.equalTo(tvcView.snp_centerX)  //暂时理解 横着
        }
    }
    
    func setTvcFrame(){
        self.setMyTvc.snp.makeConstraints { (make) in
            make.top.equalTo(35)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(-10)
        }
    }
}

